package com.icexxx.code_gen_by_line;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) throws IOException {
        System.out.println("Hello World!");
        String folder = "C:\\Users\\Administrator\\Desktop\\system";
        Path path = Paths.get(folder);
        FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                String pathStr = "" + path;
                if (pathStr.endsWith(".xml")) {
                    Path pathz = Paths.get(pathStr);
                    List<String> readAllLines = Files.readAllLines(pathz, Charset.forName("UTF-8"));
                    readAllLines = content(readAllLines);
                    String out = pathStr.replace("system", "system__");
                    out = out.replace(".xml", ".java");
                    writeFile(out, readAllLines);
                }
                return super.visitFile(path, attrs);
            }
        };
        Path walkFileTree = Files.walkFileTree(path, visitor);
    }

    private static void writeFile(String out, List<String> readAllLines) {
        Path path = Paths.get(out);
        try {
            Files.write(path, readAllLines, Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<String> content(List<String> readAllLines) {
        List<String> lines = new ArrayList<>();
        if (readAllLines != null) {
            for (String string : readAllLines) {
                StringJoiner stringJoiner = new StringJoiner("", "    sum.append(\"", "\" + nl);");
                string = replace(string);
                String integral = stringJoiner.add(string).toString();
                lines.add(integral);
            }
        }
        return lines;
    }

    private static String replace(String str) {
        char[] chs = str.toCharArray();
        int length = chs.length;
        int[] flagArray = new int[length];
        String[] padding = new String[length];
        for (int i = 0, n = 0; i < chs.length; i++) {
            char chr = chs[i];
            if (chr == '\"') {
                flagArray[i] = n % 2 + 2;
                padding[i] = null;
                if (flagArray[i] == 2) {
                    padding[i] = "\\\"\" + ";
                }
                if (flagArray[i] == 3) {
                    padding[i] = " + \"\\\"";
                }
                n++;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char chr = chs[i];
            if (flagArray[i] == 0) {
                sb.append(chr);
            } else if (flagArray[i] == 2 || flagArray[i] == 3) {
                sb.append(padding[i]);
            }
        }
        return sb.toString();
    }
}
